using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.Data.Filtering;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
namespace ChuanPlus.FormControl
{
    public partial class ControlViewOrder : DevExpress.XtraEditors.XtraUserControl
    {
        
        DevExpress.Xpo.XPCollection<OrderDetails> xpDetails;
        public ControlViewOrder()
        {
            InitializeComponent();
            dateFrom.DateTime = DateTime.Now.Date;
            dateTo.DateTime = DateTime.Now.Date;
            GetResult(dateFrom.DateTime, dateTo.DateTime);

        }
        // Provides data for the Total column.
        private void gridView2_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "Total" && e.IsGetData) e.Value =
             getTotalValue(sender as ColumnView, e.ListSourceRowIndex);
        }   
     

        // Returns the total amount for a specific row.
        decimal getTotalValue(ColumnView view, int listSourceRowIndex)
        {            
            
            if (listSourceRowIndex < xpDetails.Count)
            {
                OrderDetails d = xpDetails[listSourceRowIndex];
                return Convert.ToDecimal((d.UnitPrice * d.Quantity) + d.VAT);
            }
            return 0;
        }

        private void grid_Click(object sender, EventArgs e)
        {
            
        }   

        private void gridView1_MasterRowExpanding(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowCanExpandEventArgs e)
        {
            int dataSourceRow = gridView1.GetDataSourceRowIndex(e.RowHandle);
            xpDetails = ((Order)xpColOrders[dataSourceRow]).OrderDetails;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
          GetResult(dateFrom.DateTime,dateTo.DateTime);
        }
        private void GetResult(DateTime from, DateTime to)
        {
            xpColOrders.Criteria = new BetweenOperator("OrderDate", from, to);           
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
           
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Checks whether an end-used has double clicked the row indicator.
            GridHitInfo view1Hit = gridView1.CalcHitInfo(grid.PointToClient(MousePosition));       
            
            // return if not hit on row
            if (!view1Hit.InRow)
            {
                return;
            }
            // get datasource row by row handle            
            int DSRowIndex = gridView1.GetDataSourceRowIndex(view1Hit.RowHandle);
            Order selectedOrder = xpColOrders[DSRowIndex] as Order;
            if (selectedOrder.Status == "P")
            {
                string orderID = selectedOrder.OrderID;
                // call report
                Reports.PurchaseOrder rptPO = new ChuanPlus.Reports.PurchaseOrder();
                rptPO.Parameters[0].Value = orderID;
                rptPO.ShowPreview();

                Reports.DeliveryOrder rptDO = new ChuanPlus.Reports.DeliveryOrder();
                rptDO.Parameters[0].Value = orderID;
                rptDO.ShowPreview();

            }
            else
            {
                MessageBox.Show("Cannot print unprocessed order","Message",MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            // Checks whether an end-used has double clicked the row indicator.
            GridHitInfo view1Hit = gridView1.CalcHitInfo(grid.PointToClient(MousePosition));
            if (!gridView1.GetMasterRowExpanded(view1Hit.RowHandle))
                gridView1.ExpandMasterRow(view1Hit.RowHandle);
            else
                gridView1.CollapseMasterRow(view1Hit.RowHandle);
        }
    }
}
